<?php 

class req
{
	private $_config = '';
	
	private $_data = '';
	
	private $_page = 0;
	
	private $_param = array();
	
	public function __construct()
	{
		$this->_config = basket('config/config');
	}
	
	
	public function get_data()
	{
		$data = $this->_get_table();
		
		for ($i = 1; $i < $this->_page; $i++) {
			$table = $this->_get_table();
			$data = array_merge($data, $table);
		}
		return $data;
	}
	
	private function _get_table()
	{
		$html = $this->_get_html();
		$this->_set_param($html);
		return $this->_get_table_arr($html);
	}
	
	/**
	 * 获取html实体
	 * @param string $param
	 * @return unknown
	 */
	private function _get_html($param = '')
	{
		$html = basket('http')->http_curl($this->_config['url'], $param);
		return $html;
	}
	
	/**
	 * 获取表格数据
	 * @param unknown $html
	 * @return Ambigous <multitype:, unknown>
	 */
	private function _get_table_arr($html)
	{
		static $html_dom = '';
		if (! $html_dom) {
			$html_dom = basket('simple_html_dom');
		}
		
		$html_dom->load($html);
		
		$ret = $html_dom->find('.ListRow1, .ListRow2');
		
		$table = array();
		
		foreach ($ret as $k => $tr) {
			foreach ($tr->children as $td) {
				$t = array_shift($td->nodes[0]->_);
				$table[$k][] = $t;
			}
		}
		
		$html_dom->clear();
		
		return $table;
	}
	
	/**
	 * 获取参数
	 * @param unknown $html
	 * @return Ambigous <multitype:NULL unknown , unknown>
	 */
	private function _set_param($html)
	{
		if (! $this->_page) {
			$rs = preg_match('/共(\d+)页/', $html, $m);
			$rs && $this->_page = $m[1];
		}
		
		$rs = preg_match('/id="__VIEWSTATE"\svalue="([^"]+)"/', $html, $m);
		$rs && $this->_param['__VIEWSTATE'] = $m[1];
		
		$rs = preg_match('/id="__EVENTVALIDATION"\svalue="([^"]+)"/', $html, $m);
		$rs && $this->_param['__EVENTVALIDATION'] = $m[1];
		
		$this->_param['__EVENTTARGET'] = 'ctl00$ContentPlaceHolder1$lnkBtnNext';
	}
}
